package com.gao.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 通用请求返回体
 * 返回码 int
 * 返回信息 str
 * 返回体 str payload
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<E> {
    //请求码 0成功 1失败
    private int code;
    //返回信息
    private String msg;
    //请求体
    private E data;

    public static <E> Result<E> success(E data) {
        return new Result<>(0, "操作成功", data);
    }


    public static <E> Result<E> fail(String msg) {
        return new Result<>(1, msg,null);
    }
    //通过影响行数，返回成功与失败
    public static <E> Result<E> returnForRow(int row) {
        return row==1?success(null):fail("操作失败");
    }
    //通过boolean，返回成功与失败
    public static <E> Result<E> returnForRow(boolean added) {
        return added?success(null):fail("操作失败");
    }
}
